package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.CreditCardMapper;
import com.weipai.demo.model.CreditCard;
import com.weipai.demo.service.CreditCardService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/29 16:05
 * @Version 1.0
 */
@Service
public class CreditCardServiceImpl implements CreditCardService {

    private static final Logger log = LoggerFactory.getLogger(CreditCardServiceImpl.class);

    @Autowired
    private CreditCardMapper creditCardMapper;


    //添加或修改信用卡分类
    @Override
    public Response cardClass(String designation, String attribute, Integer status, Integer id) {
        Response response = new Response();
        CreditCard creditCard = new CreditCard();
        //判断id为空时,添加
//        if (id==null){
//            creditCard.setAttribute(attribute);
//            creditCard.setDesignation(designation);
//            creditCard.setStatus(status);
//            if (creditCard!=null){
//                int insert = creditCardMapper.insert(creditCard);
//                if (insert==1){
//                    response.setCode(RetCode.SUCCESS);
//                }else {
//                    response.setCode(RetCode.REQUEST_ERROR);
//                    return response;
//                }
//            }
//        }else {
//            creditCard.setAttribute(attribute);
//            creditCard.setDesignation(designation);
//            creditCard.setStatus(status);
//            creditCard.setId(id);
//            int i = creditCardMapper.updateByPrimaryKey(creditCard);
//            if (i==1){
//                response.setCode(RetCode.UPDATESUCCESS);
//            }else {
//                response.setCode(RetCode.REQUEST_ERROR);
//                return response;
//            }
//        }
//
        return response;
    }

    //分页展示信用卡分类列表
    @Override
    public LayuiTablePagingResult pageShow(Integer page, Integer size, CreditCard creditCard) {
        LayuiTablePagingResult result = null;

        try {
            PageHelper.startPage(page, size);

            List<CreditCard> creditCards = creditCardMapper.selectAll();

            PageInfo<CreditCard> pageInfo = new PageInfo<>(creditCards);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("pageShow" + e.getMessage());
        }

        return result;
    }

    //删除信用卡消息
    @Override
    public Response deleteById(Integer id) {
        Response response = new Response();
        if (id != null) {
            int i = creditCardMapper.deleteByPrimaryKey(id);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //按照关键字查询
    @Override
    public LayuiTablePagingResult searchBykeyword(Integer page, Integer size, CreditCard creditCard) {
        LayuiTablePagingResult result = null;

        try {
            PageHelper.startPage(page, size);

            List<CreditCard> creditCards = creditCardMapper.select(creditCard);

            PageInfo<CreditCard> pageInfo = new PageInfo<>(creditCards);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return result;
    }

    //按照关键字查询
    @Override
    public LayuiTablePagingResult getCreditList(Integer page, Integer size, CreditCard creditCard) {
        LayuiTablePagingResult result = null;

        try {
            PageHelper.startPage(page, size);

            List<Map<String, Object>> creditCards = creditCardMapper.selectAllList(creditCard);

            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(creditCards);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());

        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return result;
    }

    @Override
    public Response editcredit(CreditCard creditCard) {
        Response response = new Response();
        int rest = 0;
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = outFormat.format(now);
        creditCard.setCreateTime(s);
        if (creditCard.getId() != null) {
            rest = creditCardMapper.updateByPrimaryKeySelective(creditCard);
        } else {
            creditCard.setExamineStatus(0);
            rest = creditCardMapper.insert(creditCard);
        }
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response changePublishStatus(Integer id, Integer publishStatus) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        int rest = creditCardMapper.changePublishStatus(id, publishStatus);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.RESOURCES_ADD_FAIL);
        }
        return response;
    }

    @Override
    public Response delcredit(Integer id) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        int rest = creditCardMapper.deleteByPrimaryKey(id);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.RESOURCES_ADD_FAIL);
        }
        return response;
    }

    @Override
    public Response showCredit(Integer id) {
        Response response = new Response();
        if (id == null) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        CreditCard creditCard = new CreditCard();
        creditCard.setId(id);
        Map<String, Object> rest = creditCardMapper.selectOneCredit(creditCard);
        response.setCode(RetCode.SUCCESS);
        response.setData(rest);
        return response;
    }

    @Override
    public Response recommended(Integer page,Integer size) {
        Response response = new Response();
        Integer start = (page-1)*size;
        List<Map<String,Object>> rest = creditCardMapper.selectrRecommend(start,size);
        response.setCode(RetCode.SUCCESS);
        response.setData(rest);
        return response;
    }
    @Override
    public Response hotCard() {
        Response response = new Response();
        List<Map<String,Object>> rest = creditCardMapper.hotCard();
        response.setCode(RetCode.SUCCESS);
        response.setData(rest);
        return response;
    }

}
